package com.example.baseframework.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.os.Handler
import android.os.Message
import android.util.AttributeSet
import android.view.View
import java.util.*

/**
 * author: Yitong
 * time: 2022/6/20 10:51
 */
class MusicBar : View {
    private var paint: Paint? = null
    private var rectF1: RectF? = null
    private var rectF2: RectF? = null
    private var rectF3: RectF? = null
    private var rectF4: RectF? = null
    private var rectF5: RectF? = null
    private var viewWidth = 0
    private var viewHeight = 0
    private var rectWidth = 0
    private val columnCount = 4
    private val space = 13
    private var randomHeight = 0

    @Volatile
    private var isPlayMusicBar = false
    fun isPlaying(play: Boolean) {
        isPlayMusicBar = play
        if (isPlayMusicBar) {
            invalidate()
        }
    }

    private var random: Random? = null
    private val handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            invalidate()
        }
    }

    constructor(context: Context?) : super(context) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        viewWidth = MeasureSpec.getSize(widthMeasureSpec)
        viewHeight = MeasureSpec.getSize(heightMeasureSpec)
        rectWidth = (viewWidth - space * (columnCount - 1)) / columnCount
    }

    private fun init() {
        paint = Paint()
        paint!!.color = Color.WHITE
        paint!!.style = Paint.Style.FILL
        random = Random()
        initRect()
    }

    private fun initRect() {
        rectF1 = RectF()
        rectF2 = RectF()
        rectF3 = RectF()
        rectF4 = RectF()
        rectF5 = RectF()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val left = rectWidth + space

        //music bar width and height change logic
        randomHeight = randomInt((viewHeight * 0.4).toInt(), (viewHeight * 0.6).toInt())
        rectF1!![(left * 0).toFloat(), randomHeight.toFloat(), (left * 0 + rectWidth).toFloat()] = viewHeight.toFloat()
        randomHeight = randomInt((viewHeight * 0.7).toInt(), (viewHeight * 0.5).toInt())
        rectF2!![(left * 1).toFloat(), randomHeight.toFloat(), (left * 1 + rectWidth).toFloat()] = viewHeight.toFloat()
        randomHeight = (random!!.nextInt(viewHeight) * 0.2).toInt()
        rectF3!![(left * 2).toFloat(), randomHeight.toFloat(), (left * 2 + rectWidth).toFloat()] = viewHeight.toFloat()
        randomHeight = randomInt((viewHeight * 0.5).toInt(), (viewHeight * 0.7).toInt())
        rectF4!![(left * 3).toFloat(), randomHeight.toFloat(), (left * 3 + rectWidth).toFloat()] = viewHeight.toFloat()
        canvas.drawRect(rectF1!!, paint!!)
        canvas.drawRect(rectF2!!, paint!!)
        canvas.drawRect(rectF3!!, paint!!)
        canvas.drawRect(rectF4!!, paint!!)
        if (isPlayMusicBar) {
            handler.sendEmptyMessageDelayed(0, 150)
        }
    }

    private fun randomInt(min: Int, max: Int): Int {
        return Random().nextInt(max) % (max - min + 1) + min
    }
}